package com.example.demo.http.middlewares;

import com.example.demo.services.system.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.resource.ResourceHttpRequestHandler;

import java.util.Objects;

//基础配置拦截器，必须要配置为第一个
public class BaseMiddleware implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //标记一些基础配置
        Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).setAttribute("",request.getRemoteAddr(), RequestAttributes.SCOPE_REQUEST);
        //System.out.println("handler:"+handler.getClass().getName());
        return HandlerInterceptor.super.preHandle(request, response, handler);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        //删除线程
        UserService.remove();
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
